//
//  DACollectionFiles.m
//  DAKnowledge
//
//  Created by DreamArts on 16/8/26.
//  Copyright © 2016年 lidongyang. All rights reserved.
//

#import "DACollectionFiles.h"
#import "MJExtension.h"
#import "DAHeader.h"
#import "DADataHandel.h"


@interface DACollectionFiles ()
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, assign) BOOL isContains;
@end
@implementation DACollectionFiles

+ (DACollectionFiles *)shareDACollectionFiles{
    static DACollectionFiles *collectionFiles = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        collectionFiles = [[DACollectionFiles alloc]init];
    });
    return collectionFiles;
}

/**
 *  数组转对象数组
 */
+ (NSMutableArray *)returnModelWithArray:(NSArray *)array{
    return [DACollectionFiles mj_objectArrayWithKeyValuesArray:array];
}

/**
 *  数据请求
 */

- (void)prepareDataWithUserID:(NSString *)userID URL:(NSString *)url parame:(NSDictionary *)dic Blcok:(DACollectionFilesBlock)block{
    // 异步请求数据
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
       [DADataHandel postURL:url parame:dic cookie:nil result:^(id result) {
    //       if ([result[@"result"] isKindOfClass:[NSArray class]] ) {
               NSMutableArray *array = [DACollectionFiles returnModelWithArray:result[@"result"]];
               block(array);
               //NSLog(@"_____%@", array);
           //}
       }];
        
        
    });
    
}

/**
 *  向外边提供一个接口传递model数组的接口
 *
 *  @param userID userID
 *
 *  @return modelArray
 */
- (NSMutableArray *)returnModelArrayInUserID:(NSString *)userID{
    NSString *url = DAUserCollectFiles;
    NSDictionary *dic = @{@"collectUserID":userID};
    __weak typeof(self)weakSelf = self;
    [self prepareDataWithUserID:userID URL:url parame:dic Blcok:^(NSMutableArray *array) {
        weakSelf.array = array;
    }];
    return self.array;
}

/**
 *  刷新单列类
 */
- (void)updateWithUserID:(NSString *)userID Block:(DACollectionFilesBlock)block {
    NSString *url = DAUserCollectFiles;
    NSDictionary *dic = @{@"collectUserID":userID};
    __weak typeof(self)weakSelf = self;
    [self prepareDataWithUserID:userID URL:url parame:dic Blcok:^(NSMutableArray *array) {
        NSLog(@"数组 :%@",array);
        weakSelf.array = array;
        block(array);
    }];
    
    
}

/**
 *  根据外边传递的fileID进行遍历 判断是否收藏过
 */

- (BOOL)isContainCollectionsWith:(NSString *)fileID{
    
    for (DACollectionFiles *files in self.array) {
        if (files.fileID == fileID) {
            self.isContains = YES;
            return YES;
        }else{
            self.isContains = NO;

        }
    }
     return NO;
}





@end
